﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using System.Data;
using System.Data.SqlClient;

namespace Controller
{
    public class PrivilegeController : Controller.dbConnection.ConnectionClass
    {
        public string Save(Privilege Privileges)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "up_InsertUpdatePrivilege";
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = Privileges.ID;
            command.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar).Value = Privileges.Description;
            command.Parameters.Add("@ISASSIGNEE", SqlDbType.VarChar).Value = Privileges.isAssignee;
            command.Parameters.Add("@ISBREAKDOWN", SqlDbType.VarChar).Value = Privileges.isBreakDown;
            command.Parameters.Add("@ISACTIVE", SqlDbType.VarChar).Value = Privileges.isActive;
            command.Parameters.Add("@LASTMODIFIEDBY", SqlDbType.VarChar).Value = Privileges.LastModifiedBy;
            command.Parameters.Add("@CREATEBY", SqlDbType.VarChar).Value = Privileges.CreateBy;
            command.Parameters.Add("@ISADDKPI", SqlDbType.VarChar).Value = Privileges.isAddKPI;
            command.Parameters.Add("@ISPICKKPI", SqlDbType.VarChar).Value = Privileges.isPickKPI;

            return ExecuteNonQuery(command);
        }

        public string Delete(Privilege Privileges)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "up_DeletePrivilege";
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = Privileges.ID;
            command.Parameters.Add("@LASTMODIFIEDBY", SqlDbType.VarChar).Value = Privileges.LastModifiedBy;

            return ExecuteNonQuery(command);
        }

        public IList<Privilege> GetAll(string sSearch, int PageNumber, int Datalength, string SortCol, string SortDirection, out int TotalData)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetAllPrivileges";
            command.Parameters.Add("@SEARCH", SqlDbType.VarChar).Value = sSearch;

            DataSet dx = GetTable(command);

            List<Privilege> Privileges = new List<Privilege>();

            switch (SortDirection)
            {
                case "asc":
                    switch (SortCol)
                    {
                        case "Description":
                            dx.Tables[0].DefaultView.Sort = "Description ASC";
                            break;
                        case "LastModifiedDate":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedDate ASC";
                            break;
                        case "LastModifiedBy":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedBy ASC";
                            break;
                        case "CreateDate":
                            dx.Tables[0].DefaultView.Sort = "CreateDate ASC";
                            break;
                        case "CreateBy":
                            dx.Tables[0].DefaultView.Sort = "CreateBy ASC";
                            break;
                    }
                    break;
                case "desc":
                    switch (SortCol)
                    {
                        case "Description":
                            dx.Tables[0].DefaultView.Sort = "Description DESC";
                            break;
                        case "LastModifiedDate":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedDate DESC";
                            break;
                        case "LastModifiedBy":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedBy DESC";
                            break;
                        case "CreateDate":
                            dx.Tables[0].DefaultView.Sort = "CreateDate DESC";
                            break;
                        case "CreateBy":
                            dx.Tables[0].DefaultView.Sort = "CreateBy DESC";
                            break;
                    }
                    break;
            }

            DataTable dt = dx.Tables[0].DefaultView.ToTable();

            if (dt.Rows.Count > 0)
            {
                for (int i = PageNumber; i < Datalength; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        Privilege p = new Privilege();
                        p.ID = Convert.ToInt16(dt.Rows[i]["ID"].ToString());
                        p.Description = dt.Rows[i]["Description"].ToString();
                        p.isAssignee = Convert.ToBoolean(dt.Rows[i]["isAssignee"].ToString());
                        p.isBreakDown = Convert.ToBoolean(dt.Rows[i]["isBreakDown"].ToString());
                        p.isActive = Convert.ToBoolean(dt.Rows[i]["isActive"].ToString());
                        p.LastModifiedBy = dt.Rows[i]["LastModifiedBy"].ToString();
                        p.LastModifiedDate = dt.Rows[i]["LastModifiedDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["LastModifiedDate"].ToString());
                        p.CreateBy = dt.Rows[i]["CreateBy"].ToString();
                        p.CreateDate = dt.Rows[i]["CreateDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["CreateDate"].ToString());
                        p.isAddKPI = Convert.ToBoolean(dt.Rows[i]["isAddKPI"].ToString());
                        p.isPickKPI = Convert.ToBoolean(dt.Rows[i]["isPickKPI"].ToString());
                        Privileges.Add(p);
                    }
                    else
                    {
                        break;
                    }

                }
            }

            TotalData = dt.Rows.Count;

            return Privileges.ToList();
        }

        public Privilege GetPrivilegeByID(int ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "sp_GetPrivilegeByID";
            command.Parameters.Add("@ID", SqlDbType.Int).Value = ID;

            IList<string> Data = GetSingleRow(command);
            Privilege Privileges = null;

            if (Data != null && Data.Count > 0)
            {
                Privileges = new Privilege();
                Privileges.ID = Convert.ToInt16(Data[0]);
                Privileges.Description = Data[1].ToString();
                Privileges.isAssignee = Convert.ToBoolean(Data[2]);
                Privileges.isBreakDown = Convert.ToBoolean(Data[3]);
                Privileges.isActive = Convert.ToBoolean(Data[4]);
                Privileges.LastModifiedDate = Data[5] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[5].ToString());
                Privileges.LastModifiedBy = Data[6].ToString();
                Privileges.CreateDate = Data[7] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[7].ToString());
                Privileges.CreateBy = Data[8].ToString();
                Privileges.isAddKPI = Convert.ToBoolean(Data[9]);
                Privileges.isPickKPI = Convert.ToBoolean(Data[10]);
            }

            return Privileges;
        }

        public Privilege GetPrivilegeByDescription(string Description)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetPrivilegeByDescription";
            command.Parameters.Add("@Description", SqlDbType.VarChar).Value = Description;

            IList<string> Data = GetSingleRow(command);
            Privilege Privileges = null;

            if (Data != null && Data.Count > 0)
            {
                Privileges = new Privilege();
                Privileges.ID = Convert.ToInt16(Data[0]);
                Privileges.Description = Data[1].ToString();
                Privileges.isAssignee = Convert.ToBoolean(Data[2]);
                Privileges.isBreakDown = Convert.ToBoolean(Data[3]);
                Privileges.isActive = Convert.ToBoolean(Data[4]);
                Privileges.LastModifiedDate = Data[5] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[5].ToString());
                Privileges.LastModifiedBy = Data[6].ToString();
                Privileges.CreateDate = Data[7] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[7].ToString());
                Privileges.CreateBy = Data[8].ToString();
                Privileges.isAddKPI = Convert.ToBoolean(Data[9]);
                Privileges.isPickKPI = Convert.ToBoolean(Data[10]);
            }

            return Privileges;
        }
    }
}